// 论坛板块
// by indraw 2013.01.01
package bbs

import (
    "utils/web"
    "utils/util"
    "strconv"

)


/**
 * 首页程序
 *
 * author: indraw
 * date: 2013-01-01
 */
func ForumIndex(ctx *web.Context) string {
	//初始化
	initApp(ctx)
	
	//执行操作
    forum := newForum()
    forumTop := forum.top()
    
    online := newOnline()
    onlineTop := online.top()
    
    tpl.Assign("ForumTop", forumTop)
    tpl.Assign("OnlineTop", onlineTop)
    
    tpl.Assign("OnlineNum", len(onlineTop))
    
    onlineGuest := 0
    for _, v := range onlineTop {
        if v.Userid == ""{
            onlineGuest++
        }
        
    }
    tpl.Assign("OnlineGuest", onlineGuest)
    
    return tpl.Display("forum_index.html")

}


/**
 * 板块首页
 *
 * author: indraw
 * date: 2013-01-01
 */
func ForumList(ctx *web.Context, fid string) string {
	//初始化
	initApp(ctx)
    if SysUser["isadmin"] == "0" && ctx.Params["act"] == "moder"{
        return tpl.Error("只由管理员有管理权限！")
    }
	
	//执行操作
	//fidstr,_ := strconv.Atoi(fid)
    forum := newForum()
    forum.setFid(fid)
	if err := forum.get(); err != nil {
		return tpl.Error(err.Error())
	}

    forumRoot := newForum()
    forumRoot.setFid(strconv.Itoa(forum.Fup))
	if err := forumRoot.get(); err != nil {
		return tpl.Error(err.Error())
	}
	//
    //获取分页信息
    thread := newThread()
    thread.setFid(fid)

    num := thread.count()
    var p int
    p, _ = strconv.Atoi(ctx.Params["p"])
    page := util.NewPageMaker(num, p, fid)
    
    //获取
    threadList := thread.list(page)


    tpl.Assign("ThreadList", threadList)
    tpl.Assign("ThreadNum", page)

    tpl.Assign("Forum", forum.ForumData)
    tpl.Assign("ForumRoot", forumRoot.ForumData)
    tpl.Assign("Act", ctx.Params["act"])

    //显示title
    tpl.Assign("BbsTitle", cfg["bbs"]["title"] + "-" + forum.Name)
    
    return tpl.Display("forum_list.html")

}

